package com.ridu.project.beshid.controller.api;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.api.Status;
import com.ridu.project.beshid.config.Global;
import com.ridu.project.beshid.entity.CodeEntity;
import com.ridu.project.beshid.entity.CodeLogEntity;
import com.ridu.project.beshid.entity.UserEntity;
import com.ridu.project.beshid.repository.CodeLogRepository;
import com.ridu.project.beshid.repository.CodeRepository;
import com.ridu.project.beshid.repository.UserRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * CODE
 * @author 随便 2020-05-17
 */
@RestController
@RequestMapping("/api/code")
public class CodeApiController {

    @Autowired
    private CodeRepository codeRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private CodeLogRepository codeLogRepository;

    /**
     * 使用兑换码
     */
    @PostMapping("/useCode")
    public Object useCode(String codeName, String useChannel, String token){
        UserEntity user = userRepository.findOneByToken(token);
        if(user != null && StringUtils.isNotBlank(user.getUsername())){
        } else {
            return GeneralResponse.error("请重新登陆");
        }
        CodeEntity code = codeRepository.findOneByCodeName(codeName);
        if(code != null && code.getStatus() == 0){
            code.setUserId(user.getId());
            code.setStatus(1);
            //修改用户积分
            if(user.getJifen() == null){
                user.setJifen(code.getJifen());
            } else {
                user.setJifen(user.getJifen() + code.getJifen());
            }
            userRepository.saveAndFlush(user);
            /**
             * 生成兑换日志
             */
            CodeLogEntity log = new CodeLogEntity();
            log.setCodeId(code.getId());
            log.setUseChannel(useChannel);
            log.setJifen(code.getJifen());
            log.setCurrentJifen(user.getJifen());
            log.setMobile(user.getUsername());
            log.setUserId(user.getId());
            codeLogRepository.save(log);
            return GeneralResponse.success("兑换成功，有效期" + code.getPassDay() + "天");
        }

        return GeneralResponse.error("兑换失败");
    }

    /**
     * 获取兑换码历史记录
     * @param token
     * @return
     */
    @GetMapping("/getCodeLogList")
    public Object getCodeLogList(String token){
        UserEntity user = userRepository.findOneByToken(token);
        if(user != null && StringUtils.isNotBlank(user.getUsername())){
        } else {
            return GeneralResponse.error("请重新登陆");
        }
        GeneralResponse response = new GeneralResponse<>();
        response.setData(codeLogRepository.findByUserId(user.getId()));
        return response;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Object delete(long id){
        codeRepository.delete(codeRepository.findById(id).get());
        return GeneralResponse.success("删除成功");
    }

    /**
     * 查询
     */
    @PostMapping("/find")
    public Object find(long id){
        CodeEntity code=codeRepository.findById(id).get();
        return GeneralResponse.success(code);
    }


}
